﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;

public partial class Admin_Pages_AdminList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["login"] != null)
        {
            ASPxGridView1.DataSource = new AdminUI().GetAll();
            ASPxControlsFormat.ASPxGridFormat(ASPxGridView1);
            ASPxGridView1.DataBind();
        }
        else
        {
            Response.Redirect("../Login.aspx");
        }
       
    }

    public string CheckLevelAdmin(string level)
    {
        string str = "";
        if (Convert.ToInt32(level) == 0)
        {
            str = "../Images/admin1.png";
        }
        else if(Convert.ToInt32(level) == 1)
        {
            str = "../Images/admin2.png";
        }


        return str;
    }

    public string CheckEditAdmin(string level,string username)
    {
        Admin admin = (Admin)Session["login"];

        string str = "";
        if (Convert.ToByte(level) == 0 && admin.Level ==0)
        {
            //str = "<img alt='edit' src='/Admin/Images/edit.png' width='20px' height='20px'><a href='/Admin/Pages/AdminEdit.aspx?username="+username+"'>Edit</a> <img alt='delete' src='/Admin/Images/delete.png' width='20px' height='20px'><a href='#'>Delete</a>";
            str = "<img alt='edit' src='../Images/edit.png' width='20px' height='20px'><a href='../Pages/AdminEdit.aspx?username=" + username + "'>Edit</a>";
        }
        else if (Convert.ToByte(level) == 1 && admin.Level == 0)
        {
            str = "<img alt='edit' src='../Images/edit.png' width='20px' height='20px'/><a href='../Pages/AdminEdit.aspx?username=" + username + "'>Edit</a><img alt='delete' src='../Images/delete.png' width='20px' height='20px'><a href='#' onclick=\"if(confirm('Are you sure?')){window.location='../Pages/AdminDelete.aspx?username="+username+"';}\">delete</a>";

        }
        else if (Convert.ToByte(level) == 0 && admin.Level == 1)
        {
            str = "";

        }
        if (admin.Level == 1 && Convert.ToByte(level) == 1)
        {
            str = "";
        }
        return str;
    }


    protected void lnk_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        string ca = lb.CommandArgument;
        int result = new AdminUI().Delete(ca);
        if (result != -1)
        {
            Page.RegisterStartupScript("alert", "<script>alert('delete successful');</script>");
        }
        else
        {
            Page.RegisterStartupScript("alert", "<script>alert('delete fail');</script>");
        }
        
    }

}
